package com.py.framework.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.framework.common.Constants;
import com.py.framework.common.controller.SystemController;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.entity.AreaEntity;
import com.py.framework.service.AreaService;

/**
 * 区域管理Controller
 * @author Leegern
 * @date   2018年4月20日
 */
@RestController
@RequestMapping("/area")
public class AreaController extends SystemController {
	
	@Autowired
	private AreaService areaService;
	
	/**
	 * @api {post} /area/addAreaInfo.json 添加区域实体
	 * @apiName addAreaInfo
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理
	 * @apiParam {AreaEntity} entity 区域实体
	 * @apiParam {String} areaName 区域名称
	 * @apiParam {String} areaCode 区域编码
	 * @apiParam {long} parentId 父节点ID
	 * @apiParam {int} orderBy 排序
	 * @apiParamExample {json} 请求参数示例
	 *  {"areaName":"南京市","areaCode":"10009","parentId":1,"orderBy":1}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} result
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/addAreaInfo", method = RequestMethod.POST)
	public RestServiceResult<Object> addAreaInfo(@RequestBody AreaEntity entity) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (null != entity) {
			try {
				areaService.addAreaInfo(entity);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("添加失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /area/queryAreasForPage.json 分页查询区域列表
	 * @apiName queryAreasForPage
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理 
	 * @apiParam {int} page  当前页码
	 * @apiParam {int}  limit 每页条数
	 * @apiParam {String} areaName 区域名称
	 * @apiParam {String} areaCode 区域编码
	 * @apiParam {long} areaId 区域Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} areaName 区域名称
	 * @apiSuccess {String} areaCode 区域编码
	 * @apiSuccess {long} parentId 父节点ID
	 * @apiSuccess {int} orderBy 排序
	 * @apiSuccess {String} parentAreaName 父节点名称
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":3,"data":[{"areaName":"南京市","areaCode":"10009","parentId":1,"orderBy":1,"parentAreaName":"江苏省"}]}
	 */
	@RequestMapping(value = "/queryAreasForPage", method = RequestMethod.POST)
	public RestServiceResult<List<AreaEntity>> queryAreasForPage(int page, int limit, String areaName, String areaCode, String areaId) {
		Map<String, Object> params = new HashMap<>();
		Page pager = super.getPage(page, limit);
		params.put("page", pager);
		params.put("areaName", areaName);
		params.put("areaCode", areaCode);
		params.put("parentId", areaId);
		List<AreaEntity> datas = areaService.queryAreasForPage(params);
		return super.getPageSuccessRestServiceResult(pager.getTotalCount(), datas);
	}
	
	/**
	 * @api {post} /area/getAllAreas.json 查询所有区域数据
	 * @apiName getAllAreas
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理 
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} areaName 区域名称
	 * @apiSuccess {String} areaCode 区域编码
	 * @apiSuccess {long} parentId 父节点ID
	 * @apiSuccess {int} orderBy 排序
	 * @apiSuccess {String} parentAreaName 父节点名称
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":"","count":1,"data": [{"id":1,"createTime":1525244806000,
	 *    "areaName":"江苏省","areaCode":"32000","parentId":0,"allParentId":"","orderBy":1}]}
	 */
	@RequestMapping(value = "/getAllAreas", method = RequestMethod.POST)
	public RestServiceResult<List<AreaEntity>> getAllAreas() {
		List<AreaEntity> datas = areaService.getAllAreas();
		return super.getSuccessRestServiceResult(datas);
	}
	
	/**
	 * @api {post} /area/getAreasByParentId.json 根据父Id查询区域数据
	 * @apiName getAreasByParentId
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理 
	 * @apiParam {String} parentId 父区域Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} areaName 区域名称
	 * @apiSuccess {String} areaCode 区域编码
	 * @apiSuccess {long} parentId 父节点ID
	 * @apiSuccess {int} orderBy 排序
	 * @apiSuccess {String} parentAreaName 父节点名称
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":"","count":2,"data": [{"id":1,"createTime":1525244806000,
	 *    "areaName":"江苏省","areaCode":"32000","parentId":0,"allParentId":"","orderBy":1}]}
	 */
	@RequestMapping(value = "/getAreasByParentId", method = RequestMethod.POST)
	public RestServiceResult<List<AreaEntity>> getAreasByParentId(String parentId) {
		List<AreaEntity> datas = null;
		if (StringUtils.isNotBlank(parentId)) {
			datas = areaService.getAreasByParentId(parentId);
		}
		return super.getSuccessRestServiceResult(datas);
	}
	
	/**
	 * @api {post} /area/findOneArea.json 根据区域Id获取区域信息
	 * @apiName findOneArea
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理 
	 * @apiParam {long} id 区域Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} areaName 区域名称
	 * @apiSuccess {String} areaCode 区域编码
	 * @apiSuccess {long} parentId 父节点ID
	 * @apiSuccess {int} orderBy 排序
	 * @apiSuccess {String} parentAreaName 父节点名称
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":"","count":2,"data": [{"id":1,"createTime":1525244806000,
	 *    "areaName":"江苏省","areaCode":"32000","parentId":0,"allParentId":"","orderBy":1}]}
	 */
	@RequestMapping(value = "/findOneArea", method = RequestMethod.POST)
	public RestServiceResult<AreaEntity> findOneArea(long id) {
		AreaEntity data = null;
		if (id > Constants.NUMBER_ZERO) {
			data = areaService.findOneArea(id);
		}
		return super.getSuccessRestServiceResult(data);
	}
	
	/**
	 * @api {post} /area/updateArea.json 更新区域信息
	 * @apiName updateArea
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理 
	 * @apiParam {AreaEntity} entity 区域实体
	 * @apiParam {String} areaName 区域名称
	 * @apiParam {String} areaCode 区域编码
	 * @apiParam {long} parentId 父节点ID
	 * @apiParam {int} orderBy 排序
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} result
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":"","count":0,"data": null}
	 */
	@RequestMapping(value = "/updateArea", method = RequestMethod.POST)
	public RestServiceResult<Object> updateArea(@RequestBody AreaEntity entity) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (null != entity) {
			try {
				areaService.updateArea(entity);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("更新失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /area/batchDelArea.json 批量删除区域
	 * @apiName batchDelArea
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理 
	 * @apiParam {String} ids 区域Id集合
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":"","count":0,"data": null}
	 */
	@RequestMapping(value = "/batchDelArea", method = RequestMethod.POST)
	public RestServiceResult<Object> batchDelArea(String ids) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (StringUtils.isNotBlank(ids)) {
			try {
				areaService.batchDelArea(super.convert2Long(ids));
			} catch (ServiceException e) {
				result = super.getFailedRestServiceResult(e.getMessage());
				ExceptionLogger.error(e);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("删除失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /area/isExistArea.json 根据区域编码判断是否有重复数据
	 * @apiName isExistArea
	 * @apiVersion 0.0.1
	 * @apiGroup 区域管理 
	 * @apiParam {String}  areaCode 区域编码
	 * @apiParam {long}  areaId 区域Id
	 * @apiParam {String}  true 存在重复, false 不存在重复
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} result
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":"","count":0,"data": {"flag":"true"}}
	 */
	@RequestMapping(value = "/isExistArea", method = RequestMethod.POST)
	public RestServiceResult<Map<String, String>> isExistArea(String areaCode,
			@RequestParam(value="areaId", required=false, defaultValue="0") long areaId) {
		RestServiceResult<Map<String, String>> rstData = null;
		Map<String, String> result = new HashMap<>();
		String flag = "";
		if (StringUtils.isNotBlank(areaCode)) {
			try {
				List<AreaEntity> datas = areaService.getAreasByCode(areaId, areaCode);
				if (! CollectionUtils.isEmpty(datas)) {
					flag = "true";
				} else {
					flag = "false";
				}
				result.put("flag", flag);
			} catch (Exception e) {
				rstData = super.getFailedRestServiceResult("查询失败！");
				ExceptionLogger.error(e);
			}
		}
		rstData = super.getSuccessRestServiceResult(result);
		return rstData;
	}
}